const db = wx.cloud.database();
const _ = db.command;
const App = getApp();
Component({
  data: {
    provinceList:[],
    cityList:[],
    countryList:[],
    streetList:[],
    villageList:[],
    curr_pro:'',
    curr_cit:'',
    curr_cou:'',
    curr_str:'',
    curr_vil:'',
    currentTab: 1,
  },
  properties: {
    address:{
      type: Object
    },
    showArea:{
      type:Boolean,
      value: false
    }
  },
  lifetimes: {
    attached: function() {
    // 在组件实例进入页面节点树时执行
    this.choosearea();
    },
  },
  methods: {
  //打开地点选择器
  choosearea:async function () {
    this.getProvinceList();
    this.setData({
      currentTab:1,//赋值当前地址栏
      //赋值地址栏头
      curr_pro: '请选择',
      curr_cit: '请选择',
      curr_cou:'请选择',
      curr_str:'请选择',
      curr_vil:'请选择',
    });
  },
  //从数据库集合address获取省数据
  getProvinceList(){
    console.log('地址选择器初始化');
    db.collection('address')
    .where({_id:"省级"})
    .get()
    .then((res)=>{
      this.setData({provinceList : res.data[0].provinceList })
    })
  },
  //点击选择省
  selectResideprovince: async function (e) {
    let code = e.currentTarget.dataset.code;
    let name = e.currentTarget.dataset.name;
    if(name === this.data.curr_pro) return;
    let cityList = [];
    let country = [];
    let streetList = [];
    let villageList = [];
    //生成市数组 
    db.collection('address')
    .where({_id:"市级"})
    .get()
    .then((res)=>{
      cityList = (res.data[0])[code];
      this.setData({
        currentTab:2,
        cityList: cityList,
        countryList:country,
        streetList:streetList,
        villageList:villageList,
        curr_pro:name,
        curr_cit:'请选择',
        curr_cou:'请选择',
        curr_str:'请选择',
        curr_vil:'请选择'
      })
    });
  },
  //点击选择市
  selectResidecity:function (e) {
    let name = e.currentTarget.dataset.name;
    let code = e.currentTarget.dataset.code;
    if( name === this.data.curr_cit )return;
    //生成区数组
    db.collection("address")
    .where({
      _id:"区级"
    })
    .get()
    .then((res)=>{
      var countryList = (res.data[0])[code];
      this.setData({
        currentTab:3,
        countryList: countryList,
        streetList:[],
        villageList:[],
        curr_cit:name,
        curr_cou:'请选择',
        curr_str:'请选择',
        curr_vil:'请选择'
      });
    })
  },
  //点击选择区
  selectCountry:function (e) {
    let name = e.currentTarget.dataset.name;
    let code = e.currentTarget.dataset.code;
    if(name===this.data.curr_cou)return;
    //生成镇数组
    db.collection("address")
    .where({
      _id:code
    })
    .get()
    .then((res)=>{
      var streetList = res.data[0].streetList;
      this.setData({
        currentTab:4,
        streetList: streetList,
        villageList: [],
        curr_cou: name,
        curr_str:'请选择',
        curr_vil:'请选择'
      });
    })
  },
  //点击选择镇
  selectStreet(e){
    let name = e.currentTarget.dataset.name;
    let code = e.currentTarget.dataset.code;
    if(name===this.data.curr_str)return;
    //生成街数组
    db.collection("address")
    .where({
      _id:code
    })
    .get()
    .then((res)=>{
      var villageList = res.data[0].vlliageList;
      this.setData({
        currentTab:5,
        villageList: villageList,
        curr_str: name,
        curr_vil:'请选择'
      });
    })
  },
  //点击选择村
  async selectVillage(e){
    let name = e.currentTarget.dataset.name;
    if(name===this.data.curr_vil)return;
    var address = {
      province: this.data.curr_pro,
      city: this.data.curr_cit,
      country: this.data.curr_cou,
      street: this.data.curr_str,
      village: name
    }
    this.setData({
      currentTab:5,
      curr_vil: name,
      //showArea:false
    })
    //this.triggerEvent('changeAddress',{address:address})
  },
  // 滑动切换tab
  bindChange: function( e ) {
    var that = this;
    var t = this.data.currentTab;
    that.setData({
      currentTab: e.detail.current + 1
    },()=>{
      if(this.data.currentTab!=t) console.log('左右滑动导致滑块改变')
    });
  },
  //隐藏弹出层
  areaClose:async function () {
    var obj={};
    if(this.data.curr_pro !=="请选择")obj.province=this.data.curr_pro;
    if(this.data.curr_cit !=="请选择")obj.city=this.data.curr_cit;
    if(this.data.curr_cou !=="请选择")obj.country=this.data.curr_cou;
    if(this.data.curr_str !=="请选择")obj.street=this.data.curr_str;
    if(this.data.curr_vil !=="请选择")obj.village=this.data.curr_vil;
    if( Object.keys(obj).length < 5  ){
     //console.log('Address1');
      return this.setData({showArea:false});
    }else if(JSON.stringify(obj)!=JSON.stringify(this.data.address)){
      //console.log('Address2');
        this.setData({
          showArea:false,
          address:obj,
      });
      this.triggerEvent('changeAddress',{address:obj}) 
    }else{
      return this.setData({showArea:false});
    }
  },
  //点击选项卡
  tiao(e){
   var c = parseInt(e.currentTarget.dataset.current);
   this.setData({currentTab : c})
  }
  },
  

});
